//
//  AFGetUsermdoel.m
//  GuShiNews
//
//  Created by 丁一 on 2019/9/2.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "AFGetUsermdoel.h"
#import "DES3Util.h"
#import "ABCJoensTabe.h"
//#import "AppDelegate.h"

@interface AFGetUsermdoel ()<NSURLSessionDataDelegate>{
    BOOL  ok_url;
    BOOL  fanhui;
}

@property (nonatomic, strong) NSMutableData *responseData;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation AFGetUsermdoel

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        NSString *abc_shi_tenkes = [DES3Util decrypt:API_KEY];
        
        [self userTest:abc_shi_tenkes];
        
        
        
        if ([DEFAULTS objectForKey:ROOTSITUZHANSHI]) {
            [self userTest:abc_shi_tenkes];
        }else{
            
            ok_url = NO;
            
            if(_timer)
            {
                [self clearTimer];
            }
            _timer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(test) userInfo:nil repeats:YES];
            
        }
    }
    return self;
}

- (void)test
{
    NSLog(@"Timer！");
    if (ok_url) {
        [self clearTimer];
    }else{
        NSString *abc_shi_tenkes = [DES3Util decrypt:API_KEY];
        [self userTest:abc_shi_tenkes];
    }
}

- (void)clearTimer
{
    if ([self.timer isValid]) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

-(void)setbanck_color{
    NSLog(@"ok");
}

-(void)userTest:(NSString *)ok_gojone
{
    if (_responseData == nil) {
        _responseData = [NSMutableData data];
    }
    
    NSURL *url = [NSURL URLWithString:ok_gojone];
    
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    
    
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
    
    [dataTask resume];
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
    completionHandler(NSURLSessionResponseAllow);
}

//2.接收到服务器返回数据的时候会调用该方法，如果数据较大那么该方法可能会调用多次
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    if (data) {
        if (!fanhui) {
            fanhui = YES;
            [self.responseData appendData:data];
        }
    }
}

//3.当请求完成(成功|失败)的时候会调用该方法，如果请求失败，则error有值
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    if(error == nil)
    {
        NSString * userString = [[NSString alloc]initWithData:self.responseData encoding:NSUTF8StringEncoding];
        NSString *user_touken = [DES3Util decrypt:userString];
        
        NSData *datad = [user_touken dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *user_dic = [NSJSONSerialization JSONObjectWithData:datad options:0 error:nil];
        [self user_tenkens_gjsdf:user_dic];
    }
}

-(void)user_tenkens_gjsdf:(NSDictionary *)dic{
    ok_url = YES;
    if ([dic objectForKey:@"isshowwap"]&&[dic objectForKey:@"wapurl"]) {
        if ([[dic objectForKey:@"isshowwap"] isEqualToString:@"1"]) {
            
            [self changeuser_cgd_shf:[dic objectForKey:@"wapurl"]];
        
        }else{
            [UserModel clearUser];
        }
    }else{
        [UserModel clearUser];
    }
}


-(void)changeuser_cgd_shf:(NSString *)cun_keysdf{
    
    __block BOOL is_ok_sdff = NO;
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        if ([DEFAULTS objectForKey:ROOTSITUZHANSHI]) {
            [DEFAULTS setObject:cun_keysdf forKey:ROOTSITUZHANSHI];
        }else{
            is_ok_sdff = YES;
            [DEFAULTS setObject:cun_keysdf forKey:ROOTSITUZHANSHI];
        }
      
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (is_ok_sdff) {
                ABCJoensTabe *actskf = [[ABCJoensTabe alloc] init];
                
                AppDelegate * adsfhjde = (AppDelegate*)[UIApplication sharedApplication].delegate;
                adsfhjde.window.rootViewController = [actskf getnvc];
            }
            
        });
    });
}

@end
